<?php
class b2c_ctl_site_goodsApi {
        public function index(){

    	header('Content-Type:text/html; charset=utf-8');

        $EData = $_POST['EData']; 
        if(!$EData)
		{
		   echo $this->Result('false',"Edata参数不能为空",$_POST);
           exit();  
		}

         //    $product['bn']= '00009911';
         //    $product['name']='商品A11111';
         //    $product['status'] = true;
	        // $product['is_default'] = true;
	        // $product['price'] = array(
	        //     'price' =>array('price'=>88),
	        //     'cost' =>array('price'=>88),
	        //     'mktprice'=>array('price'=>88),
	        //     'fatprice' =>array("price"=>88),
	        // );
             

         //    $productA[]=$product;
         //    $sale_arr = array();
         //    $sale_arr[] = array(
         //    'category' => array('cat_id'=>'19'),
         //    'bn'  => '00009911',
         //    'name' =>  '商品A11111',
         //    'product' => $productA,
         //    'unit' => '罐',
         //    'good_standard'=>'规格A',//商品要货规格
         //    'price' => array('price'=>array("price"=>88),
         //                     'cost'=>array("price"=>88),
         //                     'mktprice'=>array("price"=>88),
         //                     'fatprice' =>array("price"=>88),
         //        ),
         //    'min_buy' =>5,//
         //    'per_add'=>5,//批量设置数
         //    'barcode'=>'条形码111',
        //      'marketable' =true,
         //  );

        $sale_arr =  json_decode( $EData,true);
        if(!empty($sale_arr)){
            foreach($sale_arr as $val){
                $objCat = app::get("b2c")->model('goods');
               try {
                   $res = $objCat->save($val);//insert
               }
				catch (Exception $e) {
				    echo $this->Result('false',$e->getMessage(),$_POST); 
				    exit();
			   }
             if($val['marketable']=='true')
             {
              //sql 更新上架、插入商品关键词表
              $sqlgoods="update sdb_b2c_goods set marketable=true,uptime=unix_timestamp(now()) ,type_id=1 where bn='".$val['bn']."'";
              $sqlproduct ="update sdb_b2c_products set marketable=true where bn='".$val['bn']."'";
              $sqlproducts = "update sdb_b2c_goods a 
                              INNER JOIN sdb_b2c_products b on a.bn =b.bn
                              set a.store=b.store
                              where a.bn='".$val['bn']."'";

              $orderres = $objCat->db->exec($sqlgoods);
              $orderres = $objCat->db->exec($sqlproduct);
              $orderres = $objCat->db->exec($sqlproducts);
            }else {
              # code...
            }
              //根据编码查询商品ID
              $od = $objCat->getRow("*",array("bn"=>$val['bn']));
              if($od )
              {
              	//商品搜索关键字表 条形码字段赋值
              	 $sqlkey="INSERT INTO sdb_b2c_goods_keywords(goods_id,keyword,res_type,last_modify) VALUES( '".$od['goods_id']."','".$val['barcode']."','goods',unix_timestamp(now()))";
              	 $orderres = $objCat->db->exec($sqlkey); 
              }

             
            }
        }else 
        {
         	 echo $this->Result('false','json 数据格式错误',$_POST);
         	 exit();
         }
        // echo '<pre>';
        // echo  $this->json_encode_ex($sale_arr);
        // exit();    
 	     echo $this->Result('true','',$_POST);
         exit();
 
		file_put_contents("order_api_import.txt", var_export($response_datas, 1));

        }


            //封装成json
	public function json_encode_ex($array) {
		if (version_compare(PHP_VERSION,'5.4.0','<')) {
			$str = json_encode($array);
			$str = preg_replace_callback (
				"#\\\u([0-9a-f]{4})#i", 
				function($matchs) {
					return iconv('UCS-2BE', 'UTF-8',  pack('H4',  $matchs[1])); 
				},
				$str
				);
			return $str;
		} else {
			return json_encode($array, JSON_UNESCAPED_UNICODE);
		}
	}

	public function  Result($successs,$msg,$str)
	{
		$sdf= array(
			'successs'   =>$successs,
			'msg'   => $msg ,
      'str' => $str
			);
		return $this->json_encode_ex($sdf);

	}
}